package com.teamsun.system;

import com.teamsun.datasource.DynamicDataSource;
import com.teamsun.entity.VersionInfo;
import com.teamsun.mapper.VersionMapper;
import com.teamsun.mapper.VersionMySQLMapper;
import com.teamsun.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统相关服务
 */
public class SysServiceImpl implements ISysService {

    @Autowired
    private VersionMapper versionMapper;

    @Autowired
    private VersionMySQLMapper versionMySQLMapper;

    @Autowired
    private DynamicDataSource dynamicDataSource;

    /**
     * 查询版本信息
     * @return
     */
    @Override
    public List<VersionInfo> queryVersionInfo(Map<String, Object> params, int startIndex, int endIndex) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        // 分页查询版本信息
        params.put("endIndex", endIndex);
        params.put("startIndex", startIndex);
        String versionId = (String) params.get("versionId");
        if(StringUtils.isNotBlank(versionId)) {
            params.put("versionId", versionId);
        }

        String dbType = dynamicDataSource.getMasterDBType();
        List<Map<String, Object>> rptInfos = new ArrayList<Map<String, Object>>();
        if(Constants.DBTYPE_MYSQL.equalsIgnoreCase(dbType)) {
            int offset = startIndex - 1;
            int rows = endIndex - offset;
            params.put("offset", offset);
            params.put("rows", rows);
            List<VersionInfo> versionInfos = versionMySQLMapper.queryVersionInfoByPage(params);
            return versionInfos;
        } else {
            List<VersionInfo> versionInfos = versionMapper.queryVersionInfoByPage(params);
            return versionInfos;
        }
    }

    /**
     * 分页查询版本信息
     * @param params
     * @param startIndex
     * @param endIndex
     * @return
     */
    @Override
    public Map<String, Object> queryVersionInfoByPage(Map<String, Object> params, int startIndex, int endIndex) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        // 分页查询版本信息
        params.put("endIndex", endIndex);
        params.put("startIndex", startIndex);
        String versionId = (String) params.get("versionId");
        if(StringUtils.isNotBlank(versionId)) {
            params.put("versionId", versionId);
        }

        String dbType = dynamicDataSource.getMasterDBType();
        List<VersionInfo> versionInfos = new ArrayList<VersionInfo>();
        int versionCount = 0;
        if(Constants.DBTYPE_MYSQL.equalsIgnoreCase(dbType)) {
            int offset = startIndex - 1;
            int rows = endIndex - offset;
            params.put("offset", offset);
            params.put("rows", rows);

            versionInfos = versionMySQLMapper.queryVersionInfoByPage(params);

            // 查询版本个数
            versionCount = versionMySQLMapper.queryVersionInfoCount(params);
        } else {
            versionInfos = versionMapper.queryVersionInfoByPage(params);

            // 查询版本个数
            versionCount = versionMapper.queryVersionInfoCount(params);
        }


        resultMap.put("rows", versionInfos);
        resultMap.put("total", versionCount);
        return resultMap;
    }

    /**
     * 查询最新版本信息
     * @return
     */
    @Override
    public VersionInfo queryLatestVersion() {
        List<VersionInfo> versionInfos = versionMapper.queryVersionInfo(null);
        if(!CollectionUtils.isEmpty(versionInfos)) {
            return versionInfos.get(0);
        } else {
            return null;
        }
    }

    @Override
    public void deleteVersionInfo(Map<String, Object> params) throws Exception {
        versionMapper.deleteVersionInfo(params);
    }

    @Override
    public void addVersionInfo(VersionInfo versionInfo) throws Exception {
        versionMapper.addVersionInfo(versionInfo);
    }

    @Override
    public void updateVersionInfo(VersionInfo versionInfo) throws Exception {
        String dbType = dynamicDataSource.getMasterDBType();
        if(Constants.DBTYPE_MYSQL.equalsIgnoreCase(dbType)) {
            versionMySQLMapper.updatVersionInfo(versionInfo);
        } else {
            versionMapper.updatVersionInfo(versionInfo);
        }
    }
}
